package com.situ.hospital.department.servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONObject;

import com.situ.hospital.department.model.DepartmentModel;
import com.situ.hospital.department.service.DepartmentService;

/**
 * Servlet implementation class DepartmentServlet
 */
@WebServlet("/DepartmentServlet")
public class DepartmentServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private DepartmentService service = new DepartmentService();

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String action = request.getParameter("action");

		switch (action) {
		case "select":
			select(request, response);
			break;
		case "add":
			add(request, response);
			break;
		case "del":
			del(request, response);
			break;
		case "selectmodel":
			selectmodel(request, response);
			break;
		case "upd":
			upd(request,response);
			break;
		case "page":
			page(request,response);
			break;
		}
	}

	private void page(HttpServletRequest request, HttpServletResponse response) {
		Map<String,Object> map=new HashMap<>();
		String name = request.getParameter("departmentname");
		String address = request.getParameter("departmentaddress");
		String limit=request.getParameter("limit");
		String curr=request.getParameter("curr");
//		System.out.println(name);
		DepartmentModel department = new DepartmentModel();
		department.setDepartmentName(name);
		department.setDepartmentAddress(address);
		department.setPageLimit(Integer.parseInt(limit));
		department.setPageIndex(Integer.parseInt(curr));
		department.setPageOn(true);
		try {
			map.put("list",service.select(department));
			map.put("count", service.selectCount(department));
			JSONObject jso = new JSONObject(map);
			response.getWriter().print(jso);
		} catch (SQLException | IOException e) {
			e.printStackTrace();
		}
	}

	private void upd(HttpServletRequest request, HttpServletResponse response) {
		String updname = request.getParameter("departmentname");
		String updaddress = request.getParameter("departmentaddress");
		String updtel = request.getParameter("departmenttel");
		if (updname != null && updaddress != null && updtel != null) {
			DepartmentModel upddepartment = new DepartmentModel(updname, updaddress, updtel);
			try {
				response.getWriter().write(String.valueOf(service.update(upddepartment)));
			} catch (IOException | SQLException e) {
				e.printStackTrace();
			}
		} else {
			try {
				response.getWriter().write("2");
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
	}

	private void selectmodel(HttpServletRequest request, HttpServletResponse response) {
		String selectname = request.getParameter("departmentname");
		DepartmentModel selectDepartment = new DepartmentModel();
		selectDepartment.setDepartmentName(selectname);
		try {
			JSONObject jso = new JSONObject(service.selectModel(selectDepartment));
			response.getWriter().print(jso);
		} catch (SQLException | IOException e) {
			e.printStackTrace();
		}
		
	}

	void del(HttpServletRequest request, HttpServletResponse response) {
		String delname = request.getParameter("departmentname");
		DepartmentModel deldepartment = new DepartmentModel();
		deldepartment.setDepartmentName(delname);
		try {
			response.getWriter().write(String.valueOf(service.del(deldepartment)));
		} catch (SQLException | IOException e) {
			e.printStackTrace();
		}
		
	}

	void select(HttpServletRequest request, HttpServletResponse response){
		String name = request.getParameter("departmentname");
		String address = request.getParameter("departmentaddress");
		DepartmentModel department = new DepartmentModel();
		department.setDepartmentName(name);
		department.setDepartmentAddress(address);

		Map<String, Object> map = new HashMap<>();
		try {
			map.put("list", service.select(department));
			JSONObject jso = new JSONObject(map);
			response.getWriter().print(jso);
		} catch (SQLException | IOException e) {
			e.printStackTrace();
		}
	}
	
	void add(HttpServletRequest request, HttpServletResponse response){
		String addname = request.getParameter("departmentname");
		String addaddress = request.getParameter("departmentaddress");
		String addtel = request.getParameter("departmenttel");
		if(addname!=null&&addaddress!=null&&addtel!=null) {
			DepartmentModel adddepartment = new DepartmentModel(addname, addaddress, addtel);
			try {
				response.getWriter().write(String.valueOf(service.add(adddepartment)));
			} catch (IOException | SQLException e) {
				e.printStackTrace();
			}
		}else {
			try {
				response.getWriter().write("2");
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
	}

}
